// preload.js
// 它拥有与Chrome扩展一样的沙盒。
const { contextBridge, ipcRenderer } = require('electron');

window.addEventListener('DOMContentLoaded', () => {
    const replaceText = (selector, text) => {
        const element = document.getElementById(selector);
        if (element) element.innerText = text;
    };

    for (const dependency of ['chrome', 'node', 'electron']) {
        replaceText(`${dependency}-version`, process.versions[dependency]);
    }
});

contextBridge.exposeInMainWorld('electronAPI', {
    // map tiles interfaces
    CloseMapWnd: (params) => ipcRenderer.invoke('map/CloseMapWnd', params),
    DeleteCaptureImages: (params) => ipcRenderer.invoke('map/DeleteCaptureImages', params),
    GetCaptureDirs: (params) => ipcRenderer.invoke('map/GetCaptureDirs', params),
    GetCaptureImages: (params) => ipcRenderer.invoke('map/GetCaptureImages', params),
    ImportCaptures: (params) => ipcRenderer.invoke('map/ImportCaptures', params),
    LoadTileImage: (params) => ipcRenderer.invoke('map/LoadTileImage', params),
    NewMapWnd: (params) => ipcRenderer.invoke('map/NewMapWnd', params),
    SimulateDrag: (params) => ipcRenderer.invoke('map/SimulateDrag', params),
    // utility interfaces
    CaptureMap: (params) => ipcRenderer.invoke('utils/CaptureMap', params),
    DeleteDirectory: (params) => ipcRenderer.invoke('utils/DeleteDirectory', params),
    LoadImage: (params) => ipcRenderer.invoke('utils/LoadImage', params),
    Ping: (params) => ipcRenderer.invoke('utils/Ping', params),
    ToggleConsole: (params) => ipcRenderer.invoke('utils/ToggleConsole', params)
});
